﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook.AdminIstrator
{
    public partial class Categories : System.Web.UI.Page
    {
        protected static string Id = "";
        protected static bool Insert = false;
        protected static string PaId = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if(Data.Mysession.isLogin)
                    BindCategory();
                else
                    Response.Redirect("/AdminIstrator/Login.aspx");
            }
        }

        private void BindCategory()
        {
            grdCategory.DataSource = Business.CategoryService.Category_GetByAll();
            grdCategory.DataBind();

            for (int i = 0; i < grdCategory.Items.Count; i++)
            {
                if (grdCategory.Items[i].Cells[2].Text != "0")
                {
                    grdCategory.Items[i].Cells[2].Text = Business.CategoryService.Category_GetById(grdCategory.Items[i].Cells[2].Text)[0].cName;
                    ((Button)grdCategory.Items[i].FindControl("btnAddChild")).Visible = false;
                    
                }
                else
                {
                    ((ImageButton)grdCategory.Items[i].FindControl("imgBtnDelete")).Visible = false;
                    grdCategory.Items[i].Cells[2].Text = "Trang chủ";
                }
                if (grdCategory.Items[i].Cells[3].Text == "1")
                    grdCategory.Items[i].Cells[3].Text = "Enable";
                else
                    grdCategory.Items[i].Cells[3].Text = "Disable";
                
            }
        }

        protected void grdCategory_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Edit":
                    Id = strCA;
                    
                    List<Data.CategoryInfo> listU=Business.CategoryService.Category_GetById(Id);
                    if (listU[0].cpID != "0")
                        PaId = listU[0].cpID;
                    else
                        PaId = "0";
                    txtCatName.Text=listU[0].cName;
                    ddlStatus.Text=listU[0].cStatus;
                    Insert = false;
                    pnUpdate.Visible = true;
                    pnView.Visible = false;
                    break;
                case "Delete":
                    
                    if (Business.CategoryService.Category_GetById(strCA)[0].cpID != "0")
                    {
                        if (Business.ProductsService.Products_GetByCaId(strCA).Count<=0)
                        {
                            lblMess.Text = "";
                            Business.CategoryService.Category_Delete(strCA);
                            BindCategory();
                        }
                        else
                            lblMess.Text = "Danh mục đã có sản phẩm,bạn hãy xóa sản phẩm!";

                    }

                    break;
                case "AddChild":
                    txtCatName.Text = "";
                    PaId = strCA;
                    Insert = true;
                    pnUpdate.Visible = true;
                    pnView.Visible = false;
                    break;
            }

        }
        protected void Add_Click(object sender, EventArgs e)
        { 
            pnUpdate.Visible = true;
            pnView.Visible = false;
            Insert = true;
            txtCatName.Text = "";


        }

        protected void Save_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Data.CategoryInfo obj = new Data.CategoryInfo();
                obj.cID = Id;
                obj.cName = txtCatName.Text;
                obj.cStatus = ddlStatus.SelectedValue;
                obj.cpID = PaId;
                obj.cCode = "";
                if (Insert == false)
                {
                    Business.CategoryService.Category_Update(obj);
                }
                else
                {
                    Business.CategoryService.Category_Insert(obj);
                }
                BindCategory();
                pnUpdate.Visible = false;
                pnView.Visible = true;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            BindCategory();
            pnView.Visible = true;
            pnUpdate.Visible = false;
        }
    }
}